package com.chefu.system.controller;


import com.chefu.common.exception.CfException;
import com.chefu.common.pojo.PageResult;
import com.chefu.common.pojo.R;
import com.chefu.system.pojo.Module;
import com.chefu.system.pojo.Role;
import com.chefu.system.service.ModuleService;
import com.chefu.system.service.RoleService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.BindingResult;
import org.springframework.validation.FieldError;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * <p>
 * 前端控制器
 * </p>
 *
 * @author goodgoodstudy
 * @since 2021-03-30
 */
@RestController
@RequestMapping("/system/role")
public class RoleController {

    @Autowired
    private RoleService roleService;

    @Autowired
    private ModuleService moduleService;

    /**
     * 分页查询
     *
     * @param page
     * @param pageSize
     * @param roleName
     * @return
     */
    @GetMapping("/list")
    public R findByPage(@RequestParam(value = "page", defaultValue = "1") Integer page,
                        @RequestParam(value = "pageSize", defaultValue = "5") Integer pageSize,
                        @RequestParam(value = "roleName", required = false) String roleName) {

        PageResult<Role> pageResult = roleService.findByPage(page, pageSize, roleName);
        return R.ok().data("pageResult", pageResult);
    }

    /**
     * 查询所有权限
     *
     * @return
     */
    @GetMapping("/findAllModule")
    public R findAllModule() {
        List<Module> moduleList = moduleService.findAll();
        return R.ok().data("moduleList", moduleList);
    }

    /**
     * 新增角色
     *
     * @param role
     * @param bindingResult
     * @param moduleIds
     * @return
     */
    @PostMapping("/insert")
    public R insert(@Valid Role role, BindingResult bindingResult, @RequestParam("moduleIds") List<Long> moduleIds) {
        if (bindingResult.hasErrors()) {
            //取出错误信息
            String errorMsg = bindingResult.getFieldErrors().stream().map(FieldError::getDefaultMessage).collect(Collectors.joining("|"));
            System.out.println(errorMsg);
            throw new CfException(5000, errorMsg);
        }
        roleService.insert(role, moduleIds);
        return R.ok();
    }

    /**
     * 根据角色Id查询
     *
     * @param roleId
     * @return
     */
    @GetMapping("/query")
    public R query(@RequestParam("roleId") Long roleId) {
        List<Module> moduleList = moduleService.findAll();

        List<Map<String, Object>> resultList = new ArrayList<>();

        List<String> moduleIdList = moduleService.findRoleModuleByRoleId(roleId);

        System.out.println(moduleIdList);
        for (Module module : moduleList) {
            Map<String, Object> map = new HashMap<>();
            map.put("id", module.getModuleId());
            map.put("pId", module.getParentId());
            map.put("name", module.getModuleName());
            map.put("open", true);
            if (moduleIdList.contains(module.getModuleId())) {
                map.put("checked", true);
            }
            resultList.add(map);
        }

        return R.ok().data("resultList", resultList);

    }

    /**
     * 修改角色
     *
     * @param role
     * @param result
     * @param moduleIds
     * @return
     */
    @PutMapping("/update")
    public R update(@Valid Role role, BindingResult result, @RequestParam("moduleIds") List<Long> moduleIds) {
        if (result.hasErrors()) {
            //取出错误信息
            String errorMsg = result.getFieldErrors().stream().map(FieldError::getDefaultMessage).collect(Collectors.joining("|"));
            System.out.println(errorMsg);
            throw new CfException(5000, errorMsg);
        }
        roleService.updateRole(role, moduleIds);
        return R.ok();
    }

    /**
     * 根据id删除角色
     * @param roleId
     * @return
     */
    @DeleteMapping("/delete")
    public R delete(@RequestParam("roleId") Long roleId) {
        roleService.delete(roleId);
        return R.ok();
    }


}

